package com.zsr.servlet;

import com.zsr.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet {


    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.创建cookie对象
        Cookie cookie = new Cookie("key", "value");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);

        Cookie cookie2 = new Cookie("key2", "value2");
        resp.addCookie(cookie2);

        resp.getWriter().print("cookie创建成功");

    }

    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        Cookie cookie = CookieUtils.findCookie("key", cookies);
        if (cookie != null) {
            resp.getWriter().write("Cookie的值" + cookie.getValue());
        }
    }

    /**
     * 更新Cookie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*方案一：
        1、先创建一个要修改的同名（指的就是 key）的 Cookie 对象
        2、在构造器，同时赋于新的 Cookie 值。
        3、调用 response.addCookie( Cookie );*/
        Cookie cookie = new Cookie("key", "newValue1");
        resp.addCookie(cookie);

        /*方案二：
        1、先查找到需要修改的 Cookie 对象
        2、调用 setValue()方法赋于新的 Cookie 值。
        3、调用 response.addCookie()通知客户端保存修改*/

        // 1、先查找到需要修改的 Cookie 对象
        Cookie cookie2 = CookieUtils.findCookie("key2", req.getCookies());
        if (cookie2 != null) {
            // 2、调用 setValue()方法赋于新的 Cookie 值。
            cookie2.setValue("newValue2");
            // 3、调用 response.addCookie()通知客户端保存修改
            resp.addCookie(cookie2);
        }

    }

    /**
     * 默认的会话级别的 Cookie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultValue");
        cookie.setMaxAge(-1);//Max-age 为Session
        resp.addCookie(cookie);

    }

    /**
     * 马上删除一个Cookie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = CookieUtils.findCookie("key", req.getCookies());
        cookie.setMaxAge(0);
        resp.addCookie(cookie);

    }

    /**
     * 设置存活 1 个小时的 Cooie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        Cookie cookie = new Cookie("life3600", "life3600");
        cookie.setMaxAge(60 * 60); // 设置 Cookie 一小时之后被删除。无效
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活一小时的 Cookie");
    }

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie=new Cookie("path1","path1");
        cookie.setPath(req.getContextPath()+"/abc");
        resp.addCookie(cookie);
    }
}
